package com.bzg.manager.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bzg.manager.bean.ContainerInfo;
import com.bzg.manager.bean.HisStockInfo;
import com.bzg.manager.bean.OSDInfo;
import com.bzg.manager.bean.RecInfo;
import com.bzg.manager.bean.SendInfo;
import com.bzg.manager.bean.StockInfo;
import com.bzg.manager.bean.WHInfo;
import com.bzg.manager.controller.QueryCenterController;
import com.bzg.manager.dao.QueryCenterMapper;
import com.bzg.manager.exception.BaseException;
import com.bzg.manager.param.BusiQueryParam;
import com.bzg.manager.service.QueryCenterService;
import com.bzg.manager.utils.PageBean;

@Service
public class QueryCenterServiceImpl implements QueryCenterService {

	public static final Logger log = LoggerFactory.getLogger(QueryCenterController.class);
	
	@Autowired
	private QueryCenterMapper queryCenterMapper;
	
	@Override
	public PageBean<RecInfo> getRecInfo(BusiQueryParam param) throws BaseException {
		Integer count = queryCenterMapper.getRecInfoCount(param);
		List<RecInfo> list = new ArrayList<>();
		if(count != null && count >0){
			list = queryCenterMapper.getRecInfoList(param);
		}
		
		PageBean<RecInfo> pageBean = new PageBean<>();
		
		pageBean.setTotalCount(count);
		pageBean.setDataList(list);
		return pageBean;
	}

	@Override
	public PageBean<StockInfo> getStockInfo(BusiQueryParam param) throws BaseException {
		Date date = param.getDate();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String inputDate = format.format(date);
		String sysDate = format.format(new Date());
		Integer count = 0;
		List<StockInfo> list = new ArrayList<>();
		//判断日期是否为当天
		if(sysDate.equals(inputDate)){//表示查询当天的库存信息
			count= queryCenterMapper.getTodayStockInfoCount(param);
			if(count > 0){
				list = queryCenterMapper.getTodayStockInfo(param);
			}
		}else{
			count= queryCenterMapper.getBefTodayStockInfoCount(param);
			if(count > 0){
				list = queryCenterMapper.getBefTodayStockInfo(param);
			}
		}
		
		PageBean<StockInfo> pageBean = new PageBean<>();
		
		pageBean.setTotalCount(count);
		pageBean.setDataList(list);
		return pageBean;
	}

	@Override
	public PageBean<HisStockInfo> getHisStockInfo(BusiQueryParam param) throws BaseException {
		Integer count = queryCenterMapper.getHisStockInfoCount(param);
		List<HisStockInfo> list = new ArrayList<>();
		if(count != null && count > 0){
			list = queryCenterMapper.getHisStockInfo(param);
		}
		PageBean<HisStockInfo> pageBean = new PageBean<>();
		pageBean.setDataList(list);
		pageBean.setTotalCount(count);
		return pageBean;
	}

	@Override
	public PageBean<SendInfo> getSendInfo(BusiQueryParam param) {
		Integer count = queryCenterMapper.getSendInfoCount(param);
		List<SendInfo> list = new ArrayList<>();
		if(count != null && count > 0){
			list = queryCenterMapper.getSendInfoList(param);
		}
		PageBean<SendInfo> pageBean = new PageBean<>();
		pageBean.setDataList(list);
		pageBean.setTotalCount(count);
		return pageBean;
	}

	@Override
	public List<WHInfo> getAllWh() {
		List<WHInfo> list = queryCenterMapper.getAllWareHouse();
		return list;
	}

	@Override
	public PageBean<ContainerInfo> getContInfo(BusiQueryParam param) {
		Integer count = queryCenterMapper.getContCount(param);
		List<ContainerInfo> list = new ArrayList<>();
		if(count != null && count > 0){
			list = queryCenterMapper.getContList(param);
		}
		PageBean<ContainerInfo> pageBean = new PageBean<>();
		pageBean.setDataList(list);
		pageBean.setTotalCount(count);
		return pageBean;
	}

	@Override
	public PageBean<OSDInfo> getOSDInfo(BusiQueryParam param) {
		Integer count = queryCenterMapper.getOSDCount(param);
		List<OSDInfo> list = new ArrayList<>();
		if(count != null && count > 0){
			list = queryCenterMapper.getOSDList(param);
		}
		PageBean<OSDInfo> pageBean = new PageBean<>();
		pageBean.setDataList(list);
		pageBean.setTotalCount(count);
		return pageBean;
	}

}
